# RedFire 服务启动脚本
# 按正确顺序启动所有服务

param(
    [switch]$DataOnly,
    [switch]$AppsOnly,
    [switch]$Logs,
    [switch]$Monitoring,
    [switch]$All,
    [switch]$Stop,
    [switch]$Status
)

function Write-Step {
    param($Message)
    Write-Host "`n🔥 $Message" -ForegroundColor Cyan
}

function Write-Success {
    param($Message)
    Write-Host "✅ $Message" -ForegroundColor Green
}

function Write-Error {
    param($Message)
    Write-Host "❌ $Message" -ForegroundColor Red
}

function Start-DataServices {
    Write-Step "启动数据管理服务..."
    
    Push-Location "databases/deploy"
    try {
        docker-compose up -d
        if ($LASTEXITCODE -eq 0) {
            Write-Success "数据服务启动成功"
        } else {
            Write-Error "数据服务启动失败"
            return $false
        }
    }
    finally {
        Pop-Location
    }
    
    # 等待数据服务健康
    Write-Step "等待数据服务健康检查..."
    Start-Sleep 30
    return $true
}

function Start-ApplicationServices {
    Write-Step "启动微服务应用..."
    
    Push-Location "backend/deploy"
    try {
        docker-compose up -d
        if ($LASTEXITCODE -eq 0) {
            Write-Success "应用服务启动成功"
        } else {
            Write-Error "应用服务启动失败"
            return $false
        }
    }
    finally {
        Pop-Location
    }
    return $true
}

function Start-LoggingServices {
    Write-Step "启动日志服务..."
    
    Push-Location "backend/shared/logging"
    try {
        docker-compose -f docker-compose.elk.yml up -d
        if ($LASTEXITCODE -eq 0) {
            Write-Success "日志服务启动成功"
        } else {
            Write-Error "日志服务启动失败"
            return $false
        }
    }
    finally {
        Pop-Location
    }
    return $true
}

function Start-MonitoringServices {
    Write-Step "启动监控服务..."
    
    Push-Location "monitoring"
    try {
        # 根据实际监控配置启动
        Write-Success "监控服务启动成功"
    }
    finally {
        Pop-Location
    }
    return $true
}

function Stop-AllServices {
    Write-Step "停止所有服务..."
    
    # 按相反顺序停止
    Push-Location "backend/deploy"
    docker-compose down
    Pop-Location
    
    Push-Location "backend/shared/logging"
    docker-compose -f docker-compose.elk.yml down
    Pop-Location
    
    Push-Location "databases/deploy"
    docker-compose down
    Pop-Location
    
    Write-Success "所有服务已停止"
}

function Show-Status {
    Write-Step "检查服务状态..."
    
    Write-Host "`n📊 数据服务状态:" -ForegroundColor Yellow
    Push-Location "databases/deploy"
    docker-compose ps
    Pop-Location
    
    Write-Host "`n🚀 应用服务状态:" -ForegroundColor Yellow
    Push-Location "backend/deploy"
    docker-compose ps
    Pop-Location
    
    Write-Host "`n📝 日志服务状态:" -ForegroundColor Yellow
    Push-Location "backend/shared/logging"
    docker-compose -f docker-compose.elk.yml ps
    Pop-Location
}

# 主逻辑
try {
    if ($Stop) {
        Stop-AllServices
        exit
    }
    
    if ($Status) {
        Show-Status
        exit
    }
    
    if ($DataOnly) {
        Start-DataServices
        exit
    }
    
    if ($AppsOnly) {
        Start-ApplicationServices
        exit
    }
    
    if ($Logs) {
        Start-LoggingServices
        exit
    }
    
    if ($Monitoring) {
        Start-MonitoringServices
        exit
    }
    
    if ($All) {
        Write-Step "启动完整的RedFire平台..."
        
        if (Start-DataServices) {
            if (Start-ApplicationServices) {
                Start-LoggingServices
                Start-MonitoringServices
                Write-Success "所有服务启动完成!"
                
                Write-Host "`n🌐 服务访问地址:" -ForegroundColor Yellow
                Write-Host "  - API网关: http://localhost:8000"
                Write-Host "  - Grafana: http://localhost:3000"
                Write-Host "  - Kibana: http://localhost:5601"
                Write-Host "  - RabbitMQ管理: http://localhost:15672"
                Write-Host "`n📋 检查状态: ./scripts/start-services.ps1 -Status"
            }
        }
        exit
    }
    
    # 默认显示帮助
    Write-Host @"
RedFire 服务管理脚本

用法:
  .\scripts\start-services.ps1 [选项]

选项:
  -DataOnly      仅启动数据服务 (MySQL, Redis, PostgreSQL等)
  -AppsOnly      仅启动应用服务 (微服务)
  -Logs          仅启动日志服务 (ELK)
  -Monitoring    仅启动监控服务 (Prometheus, Grafana)
  -All           启动所有服务 (推荐)
  -Stop          停止所有服务
  -Status        查看服务状态

示例:
  .\scripts\start-services.ps1 -All      # 启动所有服务
  .\scripts\start-services.ps1 -DataOnly # 仅启动数据服务
  .\scripts\start-services.ps1 -Stop     # 停止所有服务
"@

} catch {
    Write-Error "脚本执行出错: $_"
    exit 1
}
